package util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 轻文轻小说帮助类
 * @author ThinkPad User
 *
 */

public class IQinghelper {
	/**
	 * 将单个content.json文件读入byte[]
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static byte[] getFile(String fileName,String formatType,String jsonValue) throws IOException{
		File file = new File(fileName); 
		//预先定义输出byte[]
		//byte[] outputbuffs= new byte[1024];
		byte[] outputbuff= null;
		try {
			//获取文件
			InputStream inputStream=null;
			try{
				inputStream=new FileInputStream(file);
				//捕获异常防止程序中断
			}catch(FileNotFoundException e){
				
			}
			//文件存在
			if(inputStream!=null){
				//将文件读入ByteArrayOutputStream
				byte[] buf = new byte[1024];
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				for (int i; (i = inputStream.read(buf)) != -1;) {
					baos.write(buf, 0, i);
				}
				inputStream.close();
				//格式化utf-8防止乱码
				String data = baos.toString(formatType);
				//将Sting转成jsonArray
				JSONArray jsonArray=JSONArray.fromObject(data);
				String outputString="";
				for(int i=0;i<jsonArray.size();i++){
					JSONObject jsonObject=(JSONObject) jsonArray.get(i);
					outputString=outputString+(String) jsonObject.get(jsonValue);
				}
				//将单个文件的String内容装入byte[]
				outputbuff=outputString.getBytes();
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return outputbuff;
	}
	/**
	 * 获取输出用ByteArrayOutputStream
	 * @param begin 起始值
	 * @param end
	 * @param index
	 * @param consoleOut
	 * @return
	 * @throws IOException
	 */
	public static ByteArrayOutputStream getByteArrayOutputStream(int begin,int end,String index,boolean consoleOut,String formatType,String jsonValue) throws IOException{
		ByteArrayOutputStream stream= new ByteArrayOutputStream();
		for(int i=begin;i<end;i++){
			byte[] outputbuff=IQinghelper.getFile(index+"chapter"+i+"/content.json",formatType,jsonValue);
			if (null!=outputbuff) {
				stream.write(outputbuff);
				Logger.debug(i);
			}	
		}
		if(consoleOut){
			Logger.debug(stream);
		}
		return stream;
		
	}
	/**
	 * 将ByteArrayOutputStream写出到文件
	 * @param stream
	 * @param outputfile
	 * @return
	 */
	public static boolean fileOutput(ByteArrayOutputStream stream,String outputfile){
		
		try{
			File file=new File(outputfile);
			FileOutputStream fileOutputStream=new FileOutputStream(file);
			//fileOutputStream.write(stream.toByteArray());
			stream.writeTo(fileOutputStream);
			stream.close();
			fileOutputStream.close();
			return true;
		}catch(Exception e){
			return false;
		}

	}
	
	public static void main(String[] args) throws IOException{		
		String index="D:/book/volume34776/";
		String outputfile="d:/58.txt";
		ByteArrayOutputStream stream=getByteArrayOutputStream(180000,300000,index,true,"UTF-8","value");
		fileOutput(stream,outputfile);

	}


}
